﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using LabMS.BLL;
using LabMS.Model;
using ExtendBLL;

namespace LabMS.Web.LabPersonnel
{
    public partial class ListStaff : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("RYJBXXGL");
            strPageUserType.Add("mana");
            tbBirthMax.Attributes.Add("onclick","d.showMoreDay = false;d.show(this)");
            tbBirthMin.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this)");
            if (!IsPostBack)
            {
                //checkall.Attributes.Add("onclick", "checkAllChanged(this, 'gvStaffList')");
                List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
                //绑定性别
                list = DictionaryDropdownlist("201");
                ddlStaffSex.DataSource = list;
                ddlStaffSex.DataBind();
                ddlStaffSex.Items.Insert(0, new ListItem("不限", "0"));

                //绑定文化程度
                list = DictionaryDropdownlist("education");
                ddlEducation.DataSource = list;
                ddlEducation.DataBind();
                ddlEducation.Items.Insert(0, new ListItem("不限", "0"));

                LabDropdownlist();
                BindGridViewList("");
            }
        }

        //绑定所属实验室
        protected void LabDropdownlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> LabTemp = new List<LabMS.Model.Lab>();

            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            LabTemp = lab.GetModelList("Lab_ParentID is null");

            int count = ParLab.Count, count1 = SonLab.Count;
            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                int m = 0;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        LabTemp.Insert(i + m + 1, labModel);
                        m++;
                    }
                }
            }
            ddlLab.DataSource = LabTemp;
            ddlLab.DataBind();
            ListItem item = new ListItem("全部", "0");
            ddlLab.Items.Insert(0, item);
        }

        //绑定数据字典 返回List
        protected List<LabMS.Model.Dictionary> DictionaryDropdownlist(string code)
        {
            string strwhere = "DClass= '" + code + "'";
            LabMS.BLL.Dictionary dic = new LabMS.BLL.Dictionary();

            List<LabMS.Model.Dictionary> list = new List<LabMS.Model.Dictionary>();
            list = dic.GetModelList(strwhere);
            return list;
        }

        //绑定列表
        protected void BindGridViewList(string strWhere)
        {
            ExtendBLL.Staff staff = new ExtendBLL.Staff();
            List<ExtendModule.StaffAndOther> staffList = new List<ExtendModule.StaffAndOther>();

            staffList = staff.StaffLabList(strWhere);
            gvStaffList.DataSource = staffList;
            gvStaffList.DataBind();
        }

        //删除人员
        protected void Delete_OnClick(object sender, EventArgs e)
        {
            CheckBox cb;
            LabMS.BLL.Staff staff = new LabMS.BLL.Staff();
            try
            {
                for (int i = 0; i < gvStaffList.Rows.Count; i++)
                {
                    cb = (CheckBox)gvStaffList.Rows[i].FindControl("StaffCB");
                    if (cb.Checked)
                    {
                        string SID = gvStaffList.DataKeys[i].Value.ToString();
                        staff.Delete(int.Parse(SID));
                    }
                }
                BindGridViewList(SearchString());
                LabMS.Common.JShelper.JSAlert(Page, "deleteSuccess", "删除成功!");
            }
            catch 
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "删除失败!");
            }
        }

        protected void GridViewStaff_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //判断是否为数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标移动时改变背景色
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='6699FF';this.style.color='8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
                //鼠标点击数据行时的链接
                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    e.Row.Cells[i].Attributes.Add("onDblclick", "window.location.href='StaffPreview.aspx?ID="
                        + gvStaffList.DataKeys[gvStaffList.DataKeys.Count - 1].Value.ToString() + "'");
                }
            }
        }

        //分页
        protected void GridViewStaff_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvStaffList.PageIndex = e.NewPageIndex;
            BindGridViewList(SearchString());
        }

        //搜索查询语句
        protected string SearchString()
        {
            string LabID = ddlLab.SelectedValue;
            string StaffCode = tbStaffCode.Text;
            string StaffName = tbStaffName.Text;
            string StaffSex = ddlStaffSex.SelectedValue;
            string BirthMin = tbBirthMin.Text;
            string BirthMax = tbBirthMax.Text;
            string StaffTitle = tbStaffTitle.Text;
            string Education = ddlEducation.SelectedValue;

            string strWhere = " 1=1 ";
            if (LabID != "0")
            {
                strWhere += " and  Lab_Code='" + LabID + "'";
            }
            if (!string.IsNullOrEmpty(StaffCode))
            {
                strWhere += " and  Staff_Code='" + StaffCode + "'";
            }
            if (!string.IsNullOrEmpty(StaffName))
            {
                strWhere += " and Staff_Name='" + StaffName + "'";
            }
            if (StaffSex != "0")
            {
                strWhere += " and Staff_Sex='" + StaffSex + "'";
            }
            if (!string.IsNullOrEmpty(BirthMax))
            {
                strWhere += " and Staff_Birth<='" + BirthMax + "'";
            }
            if (!string.IsNullOrEmpty(BirthMin))
            {
                strWhere += " and Staff_Birth>='" + BirthMin + "'";
            }
            if (!string.IsNullOrEmpty(StaffTitle))
            {
                strWhere += " and Staff_Title='" + StaffTitle + "'";
            }
            if (Education != "0")
            {
                strWhere += " and Staff_Education='" + Education + "'";
            }

            return strWhere;
        }

        //搜索
        protected void btSearch_Click(object sender, EventArgs e)
        {
            BindGridViewList(SearchString());
        }
    }
}
